package shangke;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class DomXml {
    /*private Document document=null;
    public void getDocument(){
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder builder=factory.newDocumentBuilder();
            document=builder.parse();
        }catch (ParserConfigurationException e){
            e.printStackTrace();
        }catch (SAXException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //创建节点
    public void createNode(){
        Element tyEl=document.createElement("kH38");
        tyEl.setAttribute("name","牛");
        Element tyCh=document.createElement("CKh38");
        tyCh.setAttribute("name","很牛");
        tyEl.appendChild(tyCh);
        document.getElementsByTagName("PhoneInfo").item(0).appendChild(tyEl);
    }
    //修改节点
    public void upNode(){
        NodeList document2=document.getElementsByTagName("Brand");
        for(int i=0;i<document2.getLength();i++){
            Node brand=document2.item(i);
            Element element=(Element) brand;
            element.setAttribute("id",i+"");
        }
    }
    //删除节点
    public void delNode(){
        NodeList brands1=document.getElementsByTagName("Brand");
        for(int i=0;i<brands1.getLength();i++){
            Node node=brands1.item(0);
            Element el=(Element) node;
            if(el.getAttribute("name").equals("华为")){
                el.getParentNode().removeChild(el);
            }
        }
    }
    //遍历结点信息
   public  void selectNodes(){
        NodeList nodeList=document.getElementsByTagName("Brand");
        for(int i=0;i< nodeList.getLength();i++){
            Node node=nodeList.item(i);
            Element em=(Element)node;
            String name=em.getAttribute("name");
            NodeList childNodes=em.getChildNodes();
            for(int j=0;j<childNodes.getLength();j++){
                Node node1=childNodes.item(j);
                if(node1.getNodeType()==Node.ELEMENT_NODE){
                    Element em1=(Element)node1;
                    String name1=em1.getAttribute("name");
                    System.out.println(name1);
                }
            }
        }
    }
    public static void main(String[] args){
        DomXml domXml=new DomXml();
        domXml.getDocument();
        domXml.selectNodes();
    }
    public void savaXML(String path){
        TransformerFactory factory=TransformerFactory.newInstance();
        factory.setAttribute("indent-number", "4");
        try {
            Transformer transformer=factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//			StreamResult result=new StreamResult(new FileOutputStream(path));
            StreamResult result=new StreamResult(new OutputStreamWriter(new FileOutputStream(path), "gb2312"));
            DOMSource  source=new DOMSource(document);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }*/
}




